package com.shyroke.daydayzhuansvc.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.shyroke.daydayzhuansvc.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommitService {
    @Autowired
    private RestTemplate restTemplate;
    public String commitBaidu(String baiduUrl,List<String> urlList) {
        StringBuffer urlBuffer = new StringBuffer();
        for (String url : urlList) {
            urlBuffer.append(url + "\n");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Host", "data.zz.baidu.com");
        headers.add("User-Agent", "curl/7.12.1");
        headers.add("Content-Length", "83");
        headers.add("Content-Type", "text/plain");
        HttpEntity<String> entity = new HttpEntity<String>(urlBuffer.toString(), headers);
        String result = restTemplate.postForObject(baiduUrl, entity, String.class);
        return result;
    }

    public String commit5118(String content) {
        String result = HttpClientUtils.doPost5118("http://apis.5118.com/wyc/akey","utf-8",content);
        return result;
    }

    public  String commitBing(String bingUrl,List<String> urlList) {
        Map<String, Object> params = new HashMap<>();
        params.put("siteUrl", "https://www.daydayzhuan.com");
        params.put("urlList", urlList);

        Map<String, String> headers = new HashMap<>();
        headers.put("Host", "ssl.bing.com");
        headers.put("Content-Type", "application/json; charset=utf-8");

        HttpResponse execute = HttpRequest.post(bingUrl).addHeaders(headers).body(JSONUtil.toJsonStr(params)).execute();
        return JSONUtil.toJsonStr(execute);
    }

}
